package com.chainofresponsibility;

import java.util.Iterator;
import java.util.Random;

class Mail {
	enum GeneralDelivery {
		YES, NO1, NO2, NO3, NO4, NO5
	}

	enum Scannability {
		UNSCANNABLE, YES1, YES2, YES3, YES4
	}

	enum Readability {
		ILLEGIBLE, YES1, YES2, YES3, YES4
	}

	enum Address {
		INCORRECT, OK1, OK2, OK3, OK4, OK5, OK6
	}

	enum ReturnAddress {
		MISSING, OK1, OK2, OK3, OK4, OK5
	}

	GeneralDelivery generalDelivery;
	Scannability scannability;
	Readability readability;
	Address address;
	ReturnAddress returnAddress;

	static long counter = 0;
	long id = counter++;

	// 重写toString方法
	public String toString() {
		return "Mail " + id;
	}

	// 显示详细详细
	public String details() {
		return toString() + ", GeneralDelivery: " + generalDelivery
				+ ", Scannability: " + scannability + ", Readability: "
				+ readability + ", Address: " + address + ", ReturnAddress: "
				+ returnAddress;

	}

	static Random rand = new Random(47);

	// 创建一个随机的Mail对象，内容也随机
	public static Mail randomMail() {
		Mail mail = new Mail();
		// 调用一个Enums中的随机选取工具
		int count = GeneralDelivery.values().length;
		mail.generalDelivery = GeneralDelivery.values()[rand.nextInt(count)];
		count = Scannability.values().length;
		mail.scannability = Scannability.values()[rand.nextInt(count)];
		count = Readability.values().length;
		mail.readability = Readability.values()[rand.nextInt(count)];
		count = Address.values().length;
		mail.address = Address.values()[rand.nextInt(count)];
		count = ReturnAddress.values().length;
		mail.returnAddress = ReturnAddress.values()[rand.nextInt(count)];
		return mail;
	}

	/**
	 * 随机产生制度能够数量的信
	 * 
	 * @param count
	 *            数量
	 * @return 以迭代器返回，可以使用for-each遍历
	 */
	public static Iterable<Mail> generator(final int count) {
		return new Iterable<Mail>() {
			int n = count;

			@Override
			public Iterator<Mail> iterator() {
				return new Iterator<Mail>() {

					@Override
					public boolean hasNext() {
						return n-- > 0;
					}

					@Override
					public Mail next() {
						return randomMail();
					}

					@Override
					public void remove() {
						throw new UnsupportedOperationException();
					}
				};
			}
		};
	}
}